package library.and.xps.com.step.view.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.orhanobut.logger.Logger;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import library.and.xps.com.step.EventBusCommond;
import library.and.xps.com.step.R;
import library.and.xps.com.step.adapter.MyPagerAdapter;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.view.activity.CustomPersonInfoActivity;
import library.and.xps.com.step.view.activity.PersonChallengeActivity;
import library.and.xps.com.step.view.base.BaseFragment;

public class PersonFragment extends BaseFragment {

    ViewPager pager;
    ArrayList<Fragment> data;
    TextView tv_mypk, tv_user_name;
    ImageView iv_user_header;
    AutoRelativeLayout rl_myteam;

    @Override
    protected int getRes() {
        return R.layout.fragment_person;
    }

    @Override
    protected void init(View view) {
        pager = r2v(view, R.id.viewpager);
        tv_mypk = r2v(view, R.id.tv_mypk);
        tv_user_name = r2v(view, R.id.tv_user_name);
        tv_user_name.setOnClickListener(this);
        iv_user_header = r2v(view, R.id.iv_user_header);
        iv_user_header.setOnClickListener(this);
        rl_myteam = r2v(view, R.id.rl_myteam);
        rl_myteam.setOnClickListener(this);
        tv_mypk.setOnClickListener(this);
        r2v(view, R.id.iv_menubt).setOnClickListener(this);
        initViewPager();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getUser() == null) return;
        tv_user_name.setText(getUser().getNickname());
        CommonUtils.setupCircleImg(getActivity(), iv_user_header, getUser().getIco());

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {//显示
            if (getUser() != null) {
                tv_user_name.setText(getUser().getNickname());
                CommonUtils.setupCircleImg(getActivity(), iv_user_header, getUser().getIco());
            }
        }
    }

    @Override
    protected void setData(View view) {
        if (getUser() != null) {
            tv_user_name.setText(getUser().getNickname());
            CommonUtils.setupCircleImg(getActivity(), iv_user_header, getUser().getIco());
        }
    }

    void initViewPager() {
        data = new ArrayList<>();
        //data.add(new PedoViewFragment());
        //data.add(new OutDoorFragment());

        pager.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager(), data));
        pager.setCurrentItem(0);
        pager.setOffscreenPageLimit(3);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_mypk:
                startActivity(new Intent(getActivity(), PersonChallengeActivity.class));
                break;
            case R.id.iv_menubt:
                EventBus.getDefault().post(EventBusCommond.OPEN_DRAWERMENU);
                break;
            case R.id.iv_user_header:
                startActivity(new Intent(getActivity(), CustomPersonInfoActivity.class));
                break;
            case R.id.tv_user_name:
                startActivity(new Intent(getActivity(), CustomPersonInfoActivity.class));
                break;
            case R.id.rl_myteam:

                break;
        }
    }
}
